//
//  PubuliuViewController.m
//  瀑布流
//
//  Created by 刘家俊 on 17/4/25.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "PubuliuViewController.h"
#import <MJExtension.h>
#import <AFNetworking.h>
#import "LJJWaterflowLayout.h"
#import "PubuliuCollectionViewCell.h"
#import "PubliuModel.h"
#import "PubuliuFrame.h"
#import "PubuliuCollectionViewCell.h"
#import <CommonCrypto/CommonDigest.h>
#import <MJRefresh.h>
#import <SDImageCache.h>


#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#define RGBColor(r, g, b,x) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(x)]
#define Color(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define RandomColor Color(arc4random_uniform(255),arc4random_uniform(255),arc4random_uniform(255))

@interface PubuliuViewController () <UICollectionViewDelegate,UICollectionViewDataSource,LJJWaterflowLayoutDelegate>
{
    __block int pageNum1;
}

/* 瀑布流列表 */
@property (nonatomic, weak) UICollectionView *collectionView;
/* 数据 */
@property (nonatomic, strong) NSMutableArray *dataSource;

/* 任务管理器 */
@property (nonatomic, weak) AFHTTPSessionManager *manager;


@end

@implementation PubuliuViewController

static NSString * const collectionID = @"CollectionViewCell";

#pragma mark - 懒加载
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Color(206, 206, 206);
    
    [self setupLayout];
    
    [self setupRefresh];
}

#pragma mark - 刷新
- (void)setupRefresh
{
    self.collectionView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadNewDataSource)];
    [self.collectionView.mj_header beginRefreshing];
    

    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDateSource)];
}

#pragma mark - 创建collection布局
- (void)setupLayout
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建布局
    LJJWaterflowLayout *layout = [[LJJWaterflowLayout alloc] init];
    layout.delegate = self;
    
    //创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) collectionViewLayout:layout];
    collectionView.backgroundColor = Color(206, 206, 206);;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
    collectionView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    collectionView.scrollIndicatorInsets = collectionView.contentInset;
    
    //注册
    [collectionView registerClass:[PubuliuCollectionViewCell class] forCellWithReuseIdentifier:collectionID];
    self.collectionView = collectionView;
    
}

#pragma mark - 加载数据
- (void)reloadNewDataSource
{
    
    self.dataSource = [NSMutableArray new];
    NSArray *arr = [self dataArr];
    
    NSArray *shopArr = [PubliuModel mj_objectArrayWithKeyValuesArray:arr];
    
    NSArray *newArr = [self statusFrameWithStatues:shopArr];
    
    
    [self.dataSource addObjectsFromArray:newArr];
    
    [self.collectionView reloadData];
    
    [self.collectionView.mj_header endRefreshing];
    [self.collectionView.mj_footer endRefreshing];

}

#pragma mark - 加载更多数据
- (void)loadMoreDateSource
{
    NSArray *arr = [self dataArr];
    
    NSArray *shopArr = [PubliuModel mj_objectArrayWithKeyValuesArray:arr];
    
    NSArray *newArr = [self statusFrameWithStatues:shopArr];
    
    
    [self.dataSource addObjectsFromArray:newArr];
    
    [self.collectionView reloadData];
    
    [self.collectionView.mj_header endRefreshing];
    [self.collectionView.mj_footer endRefreshing];

}

- (NSArray *)statusFrameWithStatues:(NSArray *)statues
{
    NSMutableArray *frames = [NSMutableArray array];
    for (PubliuModel *status in statues) {
        PubuliuFrame *f = [[PubuliuFrame alloc] init];
        f.model = status;
        [frames addObject:f];
    }
    return frames;
}


- (NSString *)timeStr
{
    //获取当前时区的时间
    NSDate *date = [NSDate date];
    //NSDateFormatter对象 格式
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //给时间格式设置时区
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    /*
     systemTimeZone:返回系统时区
     defaultTimeZone:
     localTimeZone:返回本地时区，与systemTimeZone的区别在于：本地时区可以被修改，而系统时区不能修改。
     */
    [dateFormatter setTimeZone:zone];
    //设置格式YYYY:MM:dd-HH:mm:ss选取想要的格式
    [dateFormatter setDateFormat:@"YYYY:MM:dd-HH:mm:ss"];
    
    NSString *timeStr = [dateFormatter stringFromDate:date];
    
    return timeStr;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PubuliuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionID forIndexPath:indexPath];
    cell.frameModel = self.dataSource[indexPath.item];
    return cell;
}

#pragma mark - LJJWaterflowLayoutDelegate
- (CGFloat)ljj_waterflowLayout:(LJJWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth
{
    PubuliuFrame *frame = self.dataSource[index];
    return frame.cellHeight;
}

/** item左右边距 */
- (CGFloat)ljj_columnMarginInWaterflowLayout:(LJJWaterflowLayout *)waterflowLayout
{
    return 10;
}

/** item上下边距 */
- (CGFloat)ljj_rowMarginInWaterflowLayout:(LJJWaterflowLayout *)waterflowLayout
{
    return 10;
}

/** 每一行有多少列 */
- (CGFloat)ljj_columnCountIndWaterflowLayout:(LJJWaterflowLayout *)waterFlowLayout
{
    return 2;
}

/** collectionView上下左右边距 */
- (UIEdgeInsets)ljj_edgeInsetsInWaterflowLayout:(LJJWaterflowLayout *)waterflowLayout
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}


#pragma mark - 监听滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    [[SDImageCache sharedImageCache] clearMemory];
}

- (NSArray *)dataArr
{
    return @[
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"480",@"width":@"320"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"980",@"width":@"720"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"480",@"width":@"820"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"440",@"width":@"1320"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"330",@"width":@"800"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"2316",@"width":@"900"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"550",@"width":@"880"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"480",@"width":@"1320"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"1240",@"width":@"620"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"1480",@"width":@"920"},
             @{@"user_id":@"123",@"pv":@"323",@"content":@"两老汉聊天，一老汉说：“我儿子出息了，在城里大公司当经理，年薪20万，你儿子呢？”另一个说：“不知道，听说谁找到他会奖励100万。“",@"title":@"测试",@"ID":@"12312312",@"image":@"http://img.huafans.cn/data/attachment/forum/201503/26/112341fj156wzu0wool10y.jpg",@"createTime":@"2017-7-15",@"nickname":@"我是测试",@"photo":@"https://b-ssl.duitang.com/uploads/item/201411/30/20141130235656_UuEFX.jpeg",@"height":@"840",@"width":@"620"}];
}

@end
